import pygame
import random
from pygame.sprite import Sprite

class Particle(Sprite):
    def __init__(self, x, y, color):
        super().__init__()
        self.size = random.randint(2,6)
        self.image = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
        pygame.draw.circle(self.image, color, (self.size, self.size), self.size)
        self.rect = self.image.get_rect(center=(x, y))
        self.speed_x = random.uniform(-3, 3)
        self.speed_y = random.uniform(-3, 3)
        self.lifetime = random.randint(20,40)   # 帧数

    def update(self):
        self.rect.x += self.speed_x
        self.rect.y += self.speed_y
        self.lifetime -= 1
        if self.lifetime <= 0:
            self.kill()
